#include "curusermodel.h"
#include "databasemanager.h"
#include "models/operatelogmodel.h"
CurUserModel * CurUserModel::m_pInstance = NULL;
CurUserModel::CurUserModel(QObject *parent)
    : QObject(parent)
    , m_username("")
{
}

CurUserModel * CurUserModel::getInstance()
{
    if(m_pInstance == NULL)
        m_pInstance = new CurUserModel();
    return m_pInstance;
}

bool CurUserModel::login(const QString &username, const QString &password, QString &strErr)
{
    int nRole = 0;
    bool bRet = DatabaseManager::getInstance()->login(username,password,nRole,strErr);
    if(bRet)
    {
        OPERATE_LOG("登录成功  用户名:"+username+" 用户角色:"+getUserTypeStr((UserType)nRole));
        setUserName(username);
        setRole((UserType)nRole);
        emit loginSuccess();
    }
    return bRet;
}

void CurUserModel::logout()
{
    setUserName("");
    setRole((UserType)0);
    OPERATE_LOG("退出登录");
    emit logoutSuccess();
}

QString CurUserModel::getUserName() const
{
    return m_username;
}

void CurUserModel::setUserName(const QString &username)
{
    if (m_username != username)
    {
        m_username = username;
        emit usernameChanged();
    }
}

UserType CurUserModel::getRole() const
{
    return m_nRole;
}

void CurUserModel::setRole(const UserType &nRole)
{
    if (m_nRole != nRole)
    {
        m_nRole = nRole;
        emit roleChanged();
    }
}

bool CurUserModel::updatePwd(QString strOldPwd, QString strNewPwd, QString &strErr)
{
    return DatabaseManager::getInstance()->updatePwd(m_username,strOldPwd, strNewPwd,strErr);
}
